
#矩阵
M <- matrix(c(3:14),nrow=4,byrow=TRUE);
print( M );

N <- matrix(c(3:14),nrow=4,byrow=FALSE);
print( N );

# 矩阵命名
rownames = c("row1","row2","row3","row4")
colnames = c("col1","col2","col3");

P<- matrix(c(3:14),nrow=4,byrow=TRUE,dimnames = list(rownames,colnames));
print( P );

#访问矩阵 x row y col
print(P[1,3]);

print(P[4,2]);

print(P[2,])

print(P[4,])

print(P[,3])


#矩阵的运算
matrix1 <- matrix(c(3,9,-1,4,2,6),nrow=2);

matrix2 <- matrix(c(5,2,0,9,18,7),nrow=2);

result <- matrix1 + matrix2;
print( result );


result <- matrix1 - matrix2;
print( result );